package com.kaifamiao.trace;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet( "/user/sign/in/page" )
public class UserSignInPageServlet extends HttpServlet {

    @Override
    protected void doGet( HttpServletRequest request , HttpServletResponse response )
            throws ServletException, IOException {

        HttpSession session = request.getSession();

        response.setCharacterEncoding( "UTF-8" );

        response.setContentType( "text/html;charset=UTF-8" );

        PrintWriter out = response.getWriter();

        out.println( "<!DOCTYPE html>" );
        out.println( "<html>" );
        out.println( "<head>" );
        out.println( "<meta charset='UTF-8'>" );
        out.println( "<title>用户登录</title>" );
        out.println( "</head>" );
        out.println( "<body>" );
        out.println( "<h3>用户登录</h3>" );
        out.println( "<form action='/user/sign/in/action' method='post' >" );

        Object uname = session.getAttribute( "username" );
        out.println( "<input type='text' name='username' value='" + ( uname == null ? "" : uname ) + "' placeholder='登录名称'>" );
        session.removeAttribute( "username" );

        out.println( "<input type='password' name='password' placeholder='登录密码'>" );
        out.println( "<button type='submit'>登录</button>" );
        out.println( "</form>" );

        // 尝试从会话对象中获取提示信息
        Object msg = session.getAttribute( "message" );
        out.println( "<p style='color: red;'>" + ( msg == null ? "" : msg ) + "</p>" );
        // 从会话对象中移除提示信息
        session.removeAttribute( "message" );

        out.println( "</body>" );
        out.println( "</html>" );

    }

}
